package com.agritech.smartfarming.ui;

import com.agritech.smartfarming.model.User;
import com.agritech.smartfarming.model.UserRole;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 用户管理对话框，用于添加和编辑用户
 */
public class UserDialog extends JDialog {
    private JTextField userIdField;
    private JTextField usernameField;
    private JPasswordField passwordField;
    private JComboBox<UserRole> roleComboBox;
    private JTextField emailField;
    private JTextField phoneField;
    private JCheckBox activeCheckBox;
    
    private User user;
    private boolean confirmed = false;
    
    /**
     * 创建用户对话框
     * @param parent 父窗口
     * @param title 对话框标题
     * @param user 要编辑的用户，如果为null则为添加新用户
     */
    public UserDialog(JFrame parent, String title, User user) {
        super(parent, title, true);
        this.user = user;
        
        initComponents();
        
        if (user != null) {
            // 编辑模式，填充现有用户数据
            userIdField.setText(user.getUserId());
            userIdField.setEditable(false); // 不允许修改用户ID
            usernameField.setText(user.getUsername());
            passwordField.setText(user.getPassword());
            roleComboBox.setSelectedItem(user.getRole());
            emailField.setText(user.getEmail());
            phoneField.setText(user.getPhone());
            activeCheckBox.setSelected(user.isActive());
        }
        
        pack();
        setLocationRelativeTo(parent);
    }
    
    private void initComponents() {
        // 创建主面板
        JPanel mainPanel = new JPanel(new BorderLayout());
        mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        
        // 创建表单面板
        JPanel formPanel = new JPanel(new GridLayout(0, 2, 10, 5));
        
        // 用户ID
        formPanel.add(new JLabel("用户ID:"));
        userIdField = new JTextField(20);
        formPanel.add(userIdField);
        
        // 用户名
        formPanel.add(new JLabel("用户名:"));
        usernameField = new JTextField(20);
        formPanel.add(usernameField);
        
        // 密码
        formPanel.add(new JLabel("密码:"));
        passwordField = new JPasswordField(20);
        formPanel.add(passwordField);
        
        // 角色
        formPanel.add(new JLabel("角色:"));
        roleComboBox = new JComboBox<>(UserRole.values());
        formPanel.add(roleComboBox);
        
        // 电子邮件
        formPanel.add(new JLabel("电子邮件:"));
        emailField = new JTextField(20);
        formPanel.add(emailField);
        
        // 电话
        formPanel.add(new JLabel("电话:"));
        phoneField = new JTextField(20);
        formPanel.add(phoneField);
        
        // 是否激活
        formPanel.add(new JLabel("是否激活:"));
        activeCheckBox = new JCheckBox();
        activeCheckBox.setSelected(true);
        formPanel.add(activeCheckBox);
        
        mainPanel.add(formPanel, BorderLayout.CENTER);
        
        // 创建按钮面板
        JPanel buttonPanel = new JPanel();
        JButton okButton = new JButton("确定");
        JButton cancelButton = new JButton("取消");
        
        buttonPanel.add(okButton);
        buttonPanel.add(cancelButton);
        
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);
        
        // 添加确定按钮事件
        okButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (validateInput()) {
                    confirmed = true;
                    dispose();
                }
            }
        });
        
        // 添加取消按钮事件
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                confirmed = false;
                dispose();
            }
        });
        
        setContentPane(mainPanel);
    }
    
    /**
     * 验证用户输入
     */
    private boolean validateInput() {
        String userId = userIdField.getText().trim();
        String username = usernameField.getText().trim();
        String password = new String(passwordField.getPassword()).trim();
        
        if (userId.isEmpty()) {
            JOptionPane.showMessageDialog(this, "用户ID不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        
        if (username.isEmpty()) {
            JOptionPane.showMessageDialog(this, "用户名不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        
        if (password.isEmpty()) {
            JOptionPane.showMessageDialog(this, "密码不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        
        return true;
    }
    
    /**
     * 获取用户对象
     */
    public User getUser() {
        if (!confirmed) {
            return null;
        }
        
        String userId = userIdField.getText().trim();
        String username = usernameField.getText().trim();
        String password = new String(passwordField.getPassword()).trim();
        UserRole role = (UserRole) roleComboBox.getSelectedItem();
        
        if (user == null) {
            // 创建新用户
            user = new User(userId, username, password, role);
        } else {
            // 更新现有用户
            user.setUsername(username);
            user.setPassword(password);
            user.setRole(role);
        }
        
        user.setEmail(emailField.getText().trim());
        user.setPhone(phoneField.getText().trim());
        user.setActive(activeCheckBox.isSelected());
        
        return user;
    }
    
    /**
     * 用户是否确认操作
     */
    public boolean isConfirmed() {
        return confirmed;
    }
} 